package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    @Transactional
    public void save(Employee employee, Long[] roleIds) {
        employeeMapper.insert(employee);
        // 还要往中间表, employee_role 插入给这个员工分配角色数据
        if (roleIds != null && roleIds.length > 0) {
            for (Long roleId : roleIds) {
                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }
    }

    @Override
    @Transactional
    public void delete(Long id) {
        // 还要从中间表 employee_role 中删除与这个员工相关数据
        employeeMapper.deleteRelation(id);
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(Employee employee, Long[] roleIds) {
        employeeMapper.updateByPrimaryKey(employee);
        // 从中间表 employee_role 删除旧有的关系数据
        employeeMapper.deleteRelation(employee.getId());
        // 再加入新的关系数据
        if (roleIds != null && roleIds.length > 0) {
            for (Long roleId : roleIds) {
                employeeMapper.insertRelation(employee.getId(), roleId);
            }
        }
    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Employee> employees = employeeMapper.selectForList(qo);
        return new PageInfo<>(employees);
    }

    @Override
    public boolean checkUsername(String username) {
        Employee employee = employeeMapper.checkUsername(username);
        return employee != null;
    }

    @Override
    public Employee login(String username, String password) {
        return employeeMapper.selectByUsernameAndPassword(username, password);
    }

    @Override
    public Workbook exportXls() {
        // 创建 excel 文件, 在内存中
        Workbook wb = new HSSFWorkbook();
        // 创建工作簿
        Sheet sheet = wb.createSheet("员工");
        // 创建第一行
        Row headRow = sheet.createRow(0);
        // 创建第一行第一个单元格,并设置内容
        headRow.createCell(0)
            .setCellValue("用户名");
        headRow.createCell(1)
                .setCellValue("姓名");
        headRow.createCell(2)
                .setCellValue("年龄");
        List<Employee> employees = listAll();
        for (int i = 0; i < employees.size(); i++) {
            Row row = sheet.createRow(i + 1);
            Employee employee = employees.get(i);
            row.createCell(0)
                    .setCellValue(employee.getUsername());
            row.createCell(1)
                    .setCellValue(employee.getName());
            row.createCell(2)
                    .setCellValue(employee.getAge());
        }
        return wb;
    }

    @Override
    public void importXls(Workbook wb) {
        Sheet sheet = wb.getSheetAt(0);
        // 获取这个工作簿最后一行的索引
        int lastRowNum = sheet.getLastRowNum();
        // 遍历所有的行
        for(int i = 1; i <= lastRowNum; i++) {
            Row row = sheet.getRow(i);
            String username = row.getCell(0).getStringCellValue();
            String name = row.getCell(1).getStringCellValue();
            int age = (int)row.getCell(2).getNumericCellValue();

            Employee employee = new Employee();
            employee.setUsername(username);
            employee.setName(name);
            employee.setAge(age);
            employee.setPassword("1");

            employeeMapper.insert(employee);
        }
    }
}
